package com.acc.service.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.acc.dao.BaseDao;
import com.acc.entity.Standby;
import com.acc.entity.User;
import com.acc.service.UserStandbyService;


/**
 * 
 * @author 夏艳
 *
 */


public  class UserStandbyServiceImpl extends BaseDao implements UserStandbyService{
	Connection conn=super.getConn();
	PreparedStatement pst=null;
	ResultSet rs=null;
	public List<User> showAllUser() {
		// TODO Auto-generated method stub
	
		List<User> users=new ArrayList<User>();
		String sql="select * from user_info";
		try {
			pst=conn.prepareStatement(sql);
			rs=pst.executeQuery();
			while(rs.next()) {
				User user=new User();
				
				user.setUserName(rs.getString("userName"));
				user.setGender(rs.getInt("gender"));
				
				user.setIdcard(rs.getString("idcard"));
				user.setUserId(rs.getInt("userId"));
				user.setType(rs.getInt("type"));
				users.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return users;
	}
	@Override
	public List<Standby> showAlltickets() {
		// TODO Auto-generated method stub
		List<Standby> tickets=new ArrayList<Standby>();
		String sql="select * from ti_standby";
		try {
			pst=conn.prepareStatement(sql);
			rs=pst.executeQuery();
			while(rs.next()) {
				Standby ticket=new Standby();
				ticket.setId(rs.getInt("id"));
				ticket.setStartLocation(rs.getString("start_Location"));
				ticket.setEndLocation(rs.getString("end_Location"));
				ticket.setStartTime(rs.getDate("start_time"));
				ticket.setStartDay(rs.getDate("start_day"));
				ticket.setEndTime(rs.getDate("end_time"));
				ticket.setPrice(rs.getInt("price"));
				ticket.setNum(rs.getInt("num"));
				ticket.setType(rs.getInt("type"));
				
				tickets.add(ticket);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			try {
				pst.close();
				conn.close();
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		return tickets;
	}
	@Override
	public List<Standby> queryTickets(String startLocation, String endLocation,
			Date startTime) {
		// TODO Auto-generated method stub
		List<Standby> tickets=new ArrayList<Standby>();
		String sql="select * from  ti_standby where start_Location=? and end_Location=? and start_time-?>0 and start_time-?<1";
		try {
			pst=conn.prepareStatement(sql);
			pst.setString(1, startLocation);
			pst.setString(2, endLocation);
			pst.setDate(3, startTime);
			pst.setDate(4, startTime);
			System.out.println(startTime);
			rs=pst.executeQuery();
			while(rs.next()) {
				Standby ticket=new Standby();
				ticket.setId(rs.getInt("id"));
				ticket.setStartLocation(rs.getString("start_Location"));
				ticket.setEndLocation(rs.getString("end_Location"));
				ticket.setStartTime(rs.getDate("start_time"));
				ticket.setStartDay(rs.getDate("start_day"));
				ticket.setEndTime(rs.getDate("end_time"));
				ticket.setPrice(rs.getInt("price"));
				ticket.setNum(rs.getInt("num"));
				ticket.setType(rs.getInt("type"));
				tickets.add(ticket);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			try {
				pst.close();
				conn.close();
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		return tickets;
	}
}